﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CIS470MetricsTracking
{
	public class MetricsController
	{
		public MetricsController()
		{

		}

		public ArrayList getParams(string metric_name)
		{
			ArrayList parameters = new ArrayList();
			int metricId;
			ArrayList paramIds = new ArrayList();
			string query;
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();

			// get the metric id
			query = "select metricId from dboMetricReference where actualMetric='" + metric_name + "'";
			ds = ControllerManager.data_controller.executeSelectQuery(query);
			dt = ds.Tables[0];
			metricId = (int)dt.Rows[0].ItemArray[0];

			// get the paramId's for the particular metric
			query = "select mpId from dboresultsReference where metricId=" + metricId;
			ds = ControllerManager.data_controller.executeSelectQuery(query);
			dt = ds.Tables[0];
			foreach (DataRow row in dt.Rows) {
				paramIds.Add(row.ItemArray[0]);
			}

			// obtain parameter names for the particular metric
			foreach (var m in paramIds) {
				query = "select [parameter] from dbometricParameters where mpId=" + (int)m;
				ds = ControllerManager.data_controller.executeSelectQuery(query);
				dt = ds.Tables[0];
				parameters.Add(dt.Rows[0].ItemArray[0]);
			}

			return parameters;
		}

		public int getLinkId(string metric_name, string parameter, out int metricId, out int mpId)
		{
			int linkId;
			string query;
			DataSet ds = new DataSet();
			DataTable dt = new DataTable();

			// Get the metric id first
			query = "select metricId from dboMetricReference where actualMetric='" + metric_name + "'";
			ds = ControllerManager.data_controller.executeSelectQuery(query);
			dt = ds.Tables[0];
			metricId = (int)dt.Rows[0].ItemArray[0];

			// get the parameter id next
			query = "select mpId from dbometricParameters where [parameter]='" + parameter + "'";
			ds = ControllerManager.data_controller.executeSelectQuery(query);
			dt = ds.Tables[0];
			mpId = (int)dt.Rows[0].ItemArray[0];

			// get the link id
			query = "select linkId from dboresultsReference where metricId=" + metricId + " AND " + "mpId=" + mpId;
			ds = ControllerManager.data_controller.executeSelectQuery(query);
			dt = ds.Tables[0];
			linkId = (int)dt.Rows[0].ItemArray[0];

			return linkId;
		}
	}
}
